package com.aos.util;

import com.aos.dto.PermissionDto;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TreeUtils
 * @Description:树形菜单工具类
 * @Author wumingwang
 * @Date 2018/9/17 10:57
 * @Version 1.0
 */
public class TreeUtils {

    public static List<TreeMenu> buildTree(List<PermissionDto> list,Long parentId){
        List<TreeMenu> treeList = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
            return treeList;
        }
        for(PermissionDto pd : list){
            TreeMenu treeMenu = new TreeMenu();
            if(parentId.equals(pd.getParentId())){
                String comment = pd.getComment() == null ? "" : pd.getComment();
                treeMenu.setTitle(pd.getName()+comment);
                treeMenu.setValue(String.valueOf(pd.getId()));
                if(pd.getOwnedPermission() != null){
                    treeMenu.setChecked(true);
                }
                treeMenu.setData(buildTree(list,pd.getId()));
                treeList.add(treeMenu);
            }
        }
        return treeList;
    }

    public static List<TreeMenu> buildTree(List<PermissionDto> list){
        return buildTree(list,0L);
    }
}
